package org.dong.base.thread;

import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ThreadDemo3 {
    private final ReentrantLock lock = new ReentrantLock();
    private final ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock() ;
    private final ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock() ;
    private final ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock() ;
    private static int n = 0;

    public void test() {
        new Thread(this::cal).start();
        new Thread(this::cal).start();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(n);
    }
    private void cal(){
        for (int i = 0; i < 100000; i++) {
            lock.lock();
            n = n + 1;
            lock.unlock();
        }
    }
    public static void main(String[] args) {
        new ThreadDemo3().test();
    }
}
